package com.atguigu.yygh.common.exception;

import com.atguigu.yygh.common.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice //使用aop切面编程植入我们的自定义异常处理
public class MyException {

    //全局的异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R exception(Exception e){
        e.printStackTrace();
        return R.error();
    }

    //特定的异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R exception(ArithmeticException e){
        e.printStackTrace();
        return R.error().message("特定异常");
    }

    //自定义的异常处理
    @ExceptionHandler(ziDyException.class)
    @ResponseBody
    public R exception(ziDyException e){
        e.printStackTrace();
        return R.error().message(e.getMessage()).code(e.getCode());
    }

}
